package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.pojo.Dish;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    //显示菜品分类列表
//    @GetMapping("/list")
//    public R<List<ShoppingCart>> getShopping(){
//        List<ShoppingCart> shoppingCarts = new ArrayList<>();
//        return R.success(shoppingCarts);
//    }

    //添加购物车
    @PostMapping("/add")
    public R<ShoppingCart> saveShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        //获取用户id,设置用户id
        Long userId = (Long)session.getAttribute("user");
        /* 菜品或者套餐是否点过
     如何知道烧鹅被点过了？？ 根据烧鹅的菜品id，查询下数据库中有没有烧鹅这个数据
     如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
     如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
     */
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //先判断用户点的是菜品还是套餐
        lqw.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        if(dishId != null){
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);//得到一条菜品或者套餐数据
        //判断得到的数据是否为空
        // 第一次点的时候是新增 ，后面在点击的时候是修改
        if(one == null){
            //没有数据则是新增
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }else{
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }

        return R.success(one);
    }


    //查询购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> getShoppingCart(){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(lqw);
        return R.success(shoppingCarts);
    }

    //取消购物车
    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart){
        //菜品或者套餐id 查询出当前ShoppingCart数据
        // 如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
        // 如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        Long dishId = shoppingCart.getDishId();
        if(dishId != null){
            lqw.eq(ShoppingCart::getDishId,dishId);
            //lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
            //List<ShoppingCart> dishShoppingCarts = shoppingCartService.list(lqw);
        }else{
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            //lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
            //List<ShoppingCart> setmealShoppingCarts = shoppingCartService.list(lqw);
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if(number>0){
            shoppingCartService.updateById(one);
}else {
            shoppingCartService.remove(lqw);
        }
        return R.success(one);
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> deleteShopping(){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功");
    }
}
